package com.bawei.java.thread;

/**
 * @Author : yaotianxue
 * @Time : On 2023/5/24 14:42
 * @Description : Test
 */

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/***
 * 并发：一个CPU，多个线程来回调度切换并发执行
 * 并行：3个CPU，3个线程各自占有一个CPU并行执行
 *
 * 创建三个线程方式？Thread Runnable Callable
 */
public class Test {
    public static void main(String[] args){
        //方式1：无法多继承
        new MyThread().start();
        //方式2：可以多继承，无返回值
        new Thread(new Runnable() {
            @Override
            public void run() {
            }
        }).start();
        //方式3：带有返回值
        FutureTask futureTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception { //call()方法相当与run()方法，比run方法好，有返回值
                System.out.println("call mothed begin!");
                Thread.sleep(1000 * 10);
                System.out.println("call mothed over!");

                int x = 100;
                int y = 200;

                return x + y;//自动装箱
            }
        });
        new Thread(futureTask).start();
        try {
            //得到返回结果
            Object  obj = futureTask.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
        }
    }


}
